﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using Lib.AppParamServices;
using Lib.Helper;
using Lib.MailServices;
using MSIACCSL2.Command;

namespace Lib.Mail
{
    public class MailViewModel : ViewModelBase
    {
        private readonly MailServicesClient client;
        private bool _IsBusy;
        private ObservableCollection<MailMessage> _MailMessages;
        private AppParamServices.AppParamServicesClient paramClient;
        public MailViewModel()
        {
            if (! DesignerProperties.IsInDesignTool)
            {
                client = new MailServicesClient();
                paramClient = new AppParamServicesClient();
                ReceiveCommand = new DelegateCommand(ReceiveEmail, CanReceive);
                RefreshCommand = new DelegateCommand(GetEmail, CanRefreshCommand);
                GetEmail(null);
            }
        }

        public bool IsBusy
        {
            get { return _IsBusy; }
            internal set
            {
                _IsBusy = value;
                FirePropertyChanged("IsBusy");
            }
        }

        public ObservableCollection<MailMessage> MailMessages
        {
            get { return _MailMessages; }
            set
            {
                if (MailMessages == value) return;
                _MailMessages = value;
                FirePropertyChanged("MailMessages");
            }
        }

        public ICommand ReceiveCommand { get; set; }
        public ICommand RefreshCommand { get; set; }

        public void GetEmail(Object Param)
        {
            IsBusy = true;
            paramClient.GetAppParamListCompleted += new EventHandler<GetAppParamListCompletedEventArgs>(paramClient_GetAppParamListCompleted);
            paramClient.GetAppParamListAsync("Gudang");
           
        }

        void paramClient_GetAppParamListCompleted(object sender, GetAppParamListCompletedEventArgs e)
        {
            paramClient.GetAppParamListCompleted -= new EventHandler<GetAppParamListCompletedEventArgs>(paramClient_GetAppParamListCompleted);
            client.ReadMailCompleted += Client_ReadMailCompleted;
            var KodeWarehouse = e.Result.FirstOrDefault().AppParamValue;
            client.ReadMailAsync("msiaccindorona", "brucelee2010",KodeWarehouse);
        }

        private void Client_ReadMailCompleted(object sender, ReadMailCompletedEventArgs e)
        {
            client.ReadMailCompleted -= Client_ReadMailCompleted;
            IsBusy = false;
            if (!string.IsNullOrEmpty(e.errmessage))
            {
                MessageBox.Show(e.errmessage);
                MessageBox.Show("Sistem akan mencoba sekali lagi!");
                GetEmail(null);
                return;
            }
                

            MailMessages = new ObservableCollection<MailMessage>();
            foreach (string res in e.Result)
            {
                string[] r = res.Split(new[] {";"}, StringSplitOptions.None);
                var message = new MailMessage { Subject = r[1], Tipe = r[0].Replace("DTO","") };
                MailMessages.Add(message);
            }
        }

        public bool CanReceive(Object param)
        {
            return true;
        }

        public bool CanRefreshCommand(Object param)
        {
            return true;
        }

        public void ReceiveEmail(Object param)
        {
            IsBusy = true;
            //client.ReceiveAllCompleted += client_ReceiveAllCompleted;
            //client.ReceiveAllAsync();
        }

        private void client_ReceiveAllCompleted(object sender, AsyncCompletedEventArgs e)
        {
            client.ReceiveAllCompleted -= client_ReceiveAllCompleted;
            IsBusy = false;
            MessageBox.Show("Success");
        }


        public void ReceiveOne(Object param)
        {
            IsBusy = true;
            paramClient.GetAppParamListCompleted += new EventHandler<GetAppParamListCompletedEventArgs>(paramClient_GetAppParamListCompleted2);
            paramClient.GetAppParamListAsync("Gudang",param.ToString());
          
        }

        void paramClient_GetAppParamListCompleted2(object sender, GetAppParamListCompletedEventArgs e)
        {
            paramClient.GetAppParamListCompleted -= new EventHandler<GetAppParamListCompletedEventArgs>(paramClient_GetAppParamListCompleted2);
            var KodeWarehouse = e.Result.FirstOrDefault().AppParamValue;
            client.ReceiveCompleted += new EventHandler<ReceiveCompletedEventArgs>(client_ReceiveCompleted);
            client.ReceiveAsync(e.UserState.ToString(),KodeWarehouse);
        }

        void client_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
        {
            client.ReceiveCompleted -= new EventHandler<ReceiveCompletedEventArgs>(client_ReceiveCompleted);
            if (e.Error !=null)
            {
                MessageBox.Show(e.Error.Message + ", Silahkan mencoba lagi");
                return;
            }
            client.ReceiveTrueCompleted += new EventHandler<ReceiveTrueCompletedEventArgs>(client_ReceiveTrueCompleted);
            client.ReceiveTrueAsync(e.Result, e.path);
        }

        void client_ReceiveTrueCompleted(object sender, ReceiveTrueCompletedEventArgs e)
        {
            client.ReceiveTrueCompleted -= new EventHandler<ReceiveTrueCompletedEventArgs>(client_ReceiveTrueCompleted);
            IsBusy = false;
            
            if (! string.IsNullOrEmpty(e.errMessage))
                MessageBox.Show(e.errMessage);
            else
                MessageBox.Show("Finish");
        }
    }


    public class MailMessage : ViewModelBase
    {
        private string _Subject;
        private string _Tipe;

        public string Subject
        {
            get { return _Subject; }
            internal set
            {
                _Subject = value;
                FirePropertyChanged("Subject");
            }
        }

        public string Tipe
        {
            get { return _Tipe; }
            internal set
            {
                _Tipe = value;
                FirePropertyChanged("Tipe");
            }
        }
    }
}